<template>
    <div>
        <div class="operarea">
            <div class="seach">
                <el-input v-model="seachForm.name" :placeholder="$t('name')" clearable size="mini"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="seachData"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="addDialogVisible = true">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-card class="cardBox">
            <el-table style="width: 100%" :data="fabricTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                <el-table-column prop="name" :label="$t('name')">
                </el-table-column>
                <el-table-column prop="parameter" :label="$t('parameter')">
                </el-table-column>
                <el-table-column prop="number" :label="$t('number')">
                </el-table-column>
                <el-table-column prop="preval" :label="$t('preval')">
                </el-table-column>
                <el-table-column prop="address" :width="operWidth" :label="$t('operation')">
                    <template v-slot="scope">
                        <img src="@/assets/view.png" :alt="$t('picisgone')" @click="getDetails(scope.row.id)">
                        <el-button @click="showPut(scope.row)" size="mini">{{$t('putinstorage')}}</el-button>
                        <el-button @click="showAmend(scope.row)" size="mini">{{$t('amendment')}}</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                small
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :pager-count="11"
                :current-page.sync="seachForm.pageNo"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="seachForm.pageSize"
                layout="sizes, prev, pager, next"
                :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加 -->
        <el-dialog :title="$t('add')" :visible.sync="addDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addDlogClose(addDataForm)">
            <el-form :model="addDataForm" :rules="addRules" :ref="addDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="addDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('parameter')" label-width="70px" prop="parameter">
                    <el-input v-model="addDataForm.parameter" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('number')" label-width="70px" prop="number">
                    <el-input v-model="addDataForm.number" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('preval')" label-width="70px" prop="preval">
                    <el-input v-model="addDataForm.preval" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addData(addDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 查看 -->
        <el-dialog :title="$t('examine')" :visible.sync="viewDialogVisible" width="800px" class="smallDlog" :close-on-click-modal="false">
            <el-table :data="viewTab" border style="width: 100%">
                <el-table-column prop="name" :label="$t('name')" min-width="80">
                </el-table-column>
                <el-table-column prop="parameter" :label="$t('parameter')" min-width="80">
                </el-table-column>
                <el-table-column prop="number" :label="$t('number')" min-width="80">
                </el-table-column>
                <el-table-column prop="changeType" :label="$t('changetype')" min-width="112">
                </el-table-column>
                <el-table-column prop="changeNum" :label="$t('changenum')" min-width="127">
                </el-table-column>
                <el-table-column prop="changeTime" :label="$t('changetime')" min-width="90">
                </el-table-column>
                <el-table-column prop="remark" :label="$t('remark')" min-width="80">
                </el-table-column>
            </el-table>
        </el-dialog>
        <!-- 入库 -->
        <el-dialog :title="$t('putinstorage')" :visible.sync="putDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="putDlogClose(putDataForm)">
            <el-form :model="putDataForm" :rules="putRules" :ref="putDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="putDataForm.name" :placeholder="$t('plsent')" readonly></el-input>
                </el-form-item>
                <el-form-item :label="$t('parameter')" label-width="70px" prop="parameter">
                    <el-input v-model="putDataForm.parameter" :placeholder="$t('plsent')" readonly></el-input>
                </el-form-item>
                <el-form-item :label="$t('number')" label-width="70px" prop="number">
                    <el-input v-model="putDataForm.number" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('remark')" label-width="70px" prop="remark">
                    <el-input v-model="putDataForm.remark" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="putDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="putData(putDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修正 -->
        <el-dialog :title="$t('amendment')" :visible.sync="amendDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="amendDlogClose(amendDataForm)">
            <el-form :model="amendDataForm" :rules="amendRules" :ref="amendDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="amendDataForm.name" :placeholder="$t('plsent')" readonly></el-input>
                </el-form-item>
                <el-form-item :label="$t('parameter')" label-width="70px" prop="parameter">
                    <el-input v-model="amendDataForm.parameter" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('number')" label-width="70px" prop="number">
                    <el-input v-model="amendDataForm.number" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('remark')" label-width="70px" prop="remark">
                    <el-input v-model="amendDataForm.remark" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="amendDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="amendData(amendDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import Cookie from 'js-cookie'
export default {
    data () {
        let isNumberVlidator = (rule, value, callback) => {
            let pattern = /^([0-9]{1,5})?$/
            if (!pattern.test(value)) {
                return callback(new Error(this.$t('plsentthenum')))
            } else {
                callback()
            }
        }
        return {
            total: 0,
            seachForm: {
                name: '',
                pageNo: 1,
                pageSize: 10
            },
            fabricTab: [],
            operWidth: '180',
            addDialogVisible: false,
            addDataForm: {},
            addRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                parameter: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                number: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' },
                    { validator: isNumberVlidator, trigger: 'blur' }
                ],
                preval: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' },
                    { validator: isNumberVlidator, trigger: 'blur' }
                ]
            },
            viewDialogVisible: false,
            viewTab: [],
            putDialogVisible: false,
            putDataForm: {
                name: '',
                parameter: '',
                number: '',
                remark: ''
            },
            putRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                parameter: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                number: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' },
                    { validator: isNumberVlidator, trigger: 'blur' }
                ],
                remark: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            amendDialogVisible: false,
            amendDataForm: {
                name: '',
                parameter: '',
                number: '',
                remark: ''
            },
            amendRules: {}
        }
    },
    methods: {
        seachData () {},
        handleSizeChange (newSize) {
            this.seachForm.pageSize = newSize
            this.seachData()
        },
        handleCurrentChange (newNum) {
            this.seachForm.pageNo = newNum
            this.seachData()
        },
        addDlogClose (form) {
            this.$refs[form].resetFields()
        },
        addData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                this.$message({
                    type: 'success',
                    message: this.$t('sucadd')
                })
                this.addDialogVisible = false
            })
        },
        getDetails () {
            this.viewDialogVisible = true
        },
        showPut (data) {
            this.putDataForm.name = data.name
            this.putDataForm.parameter = data.parameter
            this.putDialogVisible = true
        },
        putDlogClose (form) {
            this.$refs[form].resetFields()
        },
        putData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                this.$message({
                    type: 'success',
                    message: this.$t('waresuc')
                })
                this.putDialogVisible = false
            })
        },
        showAmend (data) {
            this.amendDataForm.name = data.name
            this.amendDataForm.parameter = data.parameter
            this.amendDialogVisible = true
        },
        amendDlogClose (form) {
            this.$refs[form].resetFields()
        },
        amendData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                this.$message({
                    type: 'success',
                    message: this.$t('revisedsuc')
                })
                this.amendDialogVisible = false
            })
        }
    },
    mounted () {
        switch (Cookie.get('language')) {
            case 'en':
                this.operWidth = '250'
                break
            case 'zh-CN':
                this.operWidth = '180'
        }
    }
}
</script>

<style lang="less" scoped>
@import './fabric.less';
</style>
